package chat.demo.server;

import chat.demo.server.handler.MyServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import sun.plugin2.message.HeartbeatMessage;

public class MyServerSocketChannelInit extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline channelPipeline = socketChannel.pipeline();
        // http decode tool
        channelPipeline.addLast(new HttpServerCodec());
        // for high data stream
        channelPipeline.addLast(new ChunkedWriteHandler());
        // cache size
        channelPipeline.addLast(new HttpObjectAggregator(1024*1024*10));
        // routing set
        channelPipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
        // netty free time check
        channelPipeline.addLast(new IdleStateHandler(100, 200, 1000));
//        // heart beat check
//        channelPipeline.addLast(new HeartbeatHandler());
        // service handler
        channelPipeline.addLast(new MyServerHandler());

    }
}
